package com.nari.ies.server.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.DecimalFormat;
import java.util.*;

@Slf4j
public class GeneraUtil {

    //根据15分钟的间隔 计算两个中间值 最后一位不要
    //新数组后三位不一样
    public static Object[] getMiddleDataByFiveMiniutesWithList(List<Object> list) {
        //根据长度，放大三倍
        int length = list.size()-1;
        int newLength=length*3;
        Object[] objects = new Object[newLength];
        for (int i = 0; i < list.size()-1; i++) {
            float cdqycNum= (Float.parseFloat(list.get(i+1).toString())-Float.parseFloat(list.get(i).toString()))/3;
            String format = new DecimalFormat(".00").format(cdqycNum);
            objects[i*3]=list.get(i);
            objects[i*3+1]=Float.parseFloat(format)+Float.parseFloat(list.get(i).toString());
            objects[i*3+2]=Float.parseFloat(format)*2+Float.parseFloat(list.get(i).toString());
        }
        return objects;
    }

    public static Object[] getDQYCData(List<LinkedHashMap<String, Object>> dqycMapList) {
        Object[] DataDQYC = new Object[96*3];
        if(dqycMapList!=null&&dqycMapList.size()>0){
            LinkedHashMap<String, Object> daqyMap = dqycMapList.get(0);
            log.info("短期预测数据："+daqyMap);
            ArrayList<Object> list = new ArrayList<>();
            Iterator<Map.Entry<String, Object>> iterator = daqyMap.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<String, Object> next = iterator.next();
                Object value = next.getValue();
                list.add(value);
            }
            for (int i = 0; i < list.size(); i++) {
                DataDQYC[i]= list.get(i);
            }
        }else {
            for (int i = 0; i < 96*3; i++) {
                DataDQYC[i]=null;
            }
        }
        return DataDQYC;
    }

    public static  Object[]  getCDQYCData(Object[] firstOfCDQYCData, ArrayList<Object> cdqycSecondlist) {
        //前后拼接
        int cdqycFirstLength=firstOfCDQYCData.length;
        int cdqycSecondLength=cdqycSecondlist.size();
        Object[] cdqycDataTotal = new Object[cdqycFirstLength+cdqycSecondLength];
        for (int i = 0; i < cdqycFirstLength; i++) {
            cdqycDataTotal[i]=firstOfCDQYCData[i];
        }
        for (int i =0; i < cdqycSecondLength; i++) {
            cdqycDataTotal[cdqycFirstLength+i]=cdqycSecondlist.get(i);
        }
        log.info("拼接后长度为"+cdqycDataTotal.length);
        //按照横轴节点取值，多余的不要
        Object[] DataCDQYC = new Object[96*3];
        for (int i = 0; i <96*3 ; i++) {
            if(i<cdqycDataTotal.length){
                DataCDQYC[i]=cdqycDataTotal[i];
            }else {
                DataCDQYC[i]=null;
            }
        }
        return  DataCDQYC;

    }

    public static ArrayList<Object> getMapToObj(ArrayList<Object> cdqycSecondlist, List<LinkedHashMap<String, Object>> cdqycDataSecond) {
        LinkedHashMap<String, Object> cdqycMap = cdqycDataSecond.get(0);
        log.info("超短期预测数据："+cdqycMap);
        Iterator<Map.Entry<String, Object>> iterator = cdqycMap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Object> next = iterator.next();
            Object value = next.getValue();
            cdqycSecondlist.add(value);
        }
        return cdqycSecondlist;
    }


    public static Object[] getValueByStep(List<Map<String, Object>> cdqycMapsList, Date startDate, Object[] cdqycDataFirst) {
        for (Map<String, Object> map : cdqycMapsList) {
            Date time = (Date) map.get("TIME");
            Object value = map.get("VALUE");

            //与初始时间间隔时常-秒级别
            long tempL = (time.getTime() - startDate.getTime()) / (60 * 1000);
            //时间间隔不满足步长要求-直接继续
            if ((tempL % 15) != 0) {
                continue;
            }
            int nPos = (int) (tempL / 15);
            if (value != null) {
                if (Float.parseFloat(String.valueOf(value)) < 0) {
                    value = 0.0;
                } else {
                    value = (float) (Math.round(Float.parseFloat(value.toString()) * 100)) / 100;
                }
            }
            cdqycDataFirst[nPos] = value;
        }
        return cdqycDataFirst;
    }
}
